<template>
	<div class="padding-across-10" v-loading.fullscreen.lock="loading">
		<el-tabs v-model="activeName" @tab-click="handleClick">
			<el-tab-pane label="绑定图书管理" name="first">
				<div style="width: 100%;padding-bottom: 3px">
					<div>
						<el-button type="primary" size="small" @click="reloadBookInfo"><i class="el-icon-eleg-icon_refresh el-icon--left"></i>刷新</el-button>
						<el-button type="primary" size="small" @click="bindBook"><i class="el-icon el-icon-plus el-icon--left"></i>添加</el-button>
						<el-button type="primary" size="small" @click="changeBookLocation"><i class="el-icon el-icon-edit el-icon--left"></i>修改馆藏地</el-button>
						<el-button type="danger" size="small" @click="deleteBook"><i class="el-icon el-icon-delete2 el-icon--left"></i>删除</el-button>
					</div>
				</div>
				<div style="width: 100%" padding="2px">
					<div style="float: left">
						<span style="font-size: 13px;font-weight: bold">检索字段</span>
						<el-select size="small" v-model="bookValue" :placeholder="$t('message.selectModeAlert')" style="width: 90px;margin-right: 2px">
							<el-option label="关键字" :value="0"></el-option>
							<el-option label="条码" :value="1"></el-option>
							<el-option label="标签" :value="2"></el-option>
						</el-select>
						<span style="font-size: 13px;font-weight: bold">关键字</span>
						<el-input size="small" style="width: 160px;" class="inline-input" v-model="bookSearchModel">
							<el-button slot="append" icon="search" @click="getBindBookInfo"></el-button>
						</el-input>
					</div>
					<div class="block" style="text-align: right;">
						<el-pagination @current-change="handleBindBookCurrentChange" @size-change="handleBindBookSizeChange" :current-page="bindBookCurrentPage" :page-size="bindBookPageSize" :page-sizes="[10,20,30,40,50]" layout="total,sizes, prev, pager, next, jumper" :total="bindBookTotalSize">
						</el-pagination>
					</div>
				</div>
				<el-table border style="width: 100%;" @selection-change="handleBindBookSelectionChange" :data="bindBookData">
					<el-table-column type="selection" align="center" width="50px"></el-table-column>
					<el-table-column label="书名" align="center" prop="Name"></el-table-column>
					<el-table-column label="作者" align="center" prop="Author"></el-table-column>
					<el-table-column label="出版社" align="center" prop="Press"></el-table-column>
					<el-table-column label="条码" align="center" prop="BookBarcode"></el-table-column>
					<el-table-column label="标签" align="center" prop="RFIDTag"></el-table-column>
					<el-table-column label="状态" align="center">
						<template slot-scope="scope">
							<el-tag type="danger" v-if="scope.row.Status === 1">丢失</el-tag>
							<el-tag type="warning" v-if="scope.row.Status === 2">外借</el-tag>
							<el-tag type="success" v-if="scope.row.Status === 3">在库</el-tag>
						</template>
					</el-table-column>
				</el-table>
				<div class="block" style="text-align: right; margin-bottom: 100px;padding-top: 2px">
					<el-pagination @current-change="handleBindBookCurrentChange" @size-change="handleBindBookSizeChange" :current-page="bindBookCurrentPage" :page-size="bindBookPageSize" :page-sizes="[10,20,30,40,50]" layout="total,sizes, prev, pager, next, jumper" :total="bindBookTotalSize">
					</el-pagination>
				</div>
			</el-tab-pane>

			<el-tab-pane label="绑定读者管理" name="second">
				<div style="width: 100%;padding-bottom: 3px">
					<div>
						<el-button type="primary" size="small" @click="reloadReaderInfo"><i class="el-icon-eleg-icon_refresh el-icon--left"></i>刷新</el-button>
						<el-button type="primary" size="small" @click="bindReader"><i class="el-icon el-icon-plus el-icon--left"></i>添加</el-button>
						<el-button type="danger" size="small" @click="deleteReader"><i class="el-icon el-icon-delete2 el-icon--left"></i>删除</el-button>
					</div>
				</div>
				<div style="width: 100%;padding: 2px">
					<div style="padding-left: 3px; float: left">
						<span style="font-size: 13px;font-weight: bold">检索字段</span>
						<el-select size="small" v-model="readerValue" placeholder="请选择查询方式" style="width: 100px;margin-right: 2px">
							<el-option label="姓名" :value="0"></el-option>
							<el-option label="卡号" :value="1"></el-option>
							<el-option label="标签" :value="2"></el-option>
							<el-option label="角色" :value="3"></el-option>
						</el-select>
						<span style="font-size: 13px;font-weight: bold">关键字</span>
						<el-input size="small" style="width: 160px;" class="inline-input" v-model="readerSearchModel">
							<el-button id="searchData" slot="append" icon="search" @click="getBindReaderInfo"></el-button>
						</el-input>
					</div>
					<div class="block" style="text-align: right;">
						<el-pagination @current-change="handleBindReaderCurrentChange" @size-change="handleBindReaderSizeChange" :current-page="bindReaderCurrentPage" :page-size="bindReaderPageSize" :page-sizes="[10,20,30,40,50]" layout="total,sizes, prev, pager, next, jumper" :total="bindReaderTotalSize">
						</el-pagination>
					</div>
				</div>
				<el-table border style="width: 100%;" @selection-change="handleBindReaderSelectionChange" :data="bindReaderData">
					<el-table-column type="selection" align="center" width="50px"></el-table-column>
					<el-table-column label="读者姓名" align="center" prop="Name"></el-table-column>
					<el-table-column label="读者证号" align="center" prop="ReaderCode"></el-table-column>
					<el-table-column label="读者标签" align="center" prop="ReaderTagCode"></el-table-column>
					<el-table-column label="角色" align="center" prop="Type"></el-table-column>
				</el-table>
				<div class="block" style="text-align: right; margin-bottom: 100px;padding-top: 2px">
					<el-pagination @current-change="handleBindReaderCurrentChange" @size-change="handleBindReaderSizeChange" :current-page="bindReaderCurrentPage" :page-size="bindReaderPageSize" :page-sizes="[10,20,30,40,50]" layout="total,sizes, prev, pager, next, jumper" :total="bindReaderTotalSize">
					</el-pagination>
				</div>
			</el-tab-pane>

			<el-tab-pane label="绑定区域管理" name="third">
				<div style="width: 100%;padding-bottom: 3px">
					<div>
						<el-button type="primary" size="small" @click="reloadDistrictInfo"><i class="el-icon-eleg-icon_refresh el-icon--left"></i>刷新</el-button>
						<el-button type="primary" size="small" @click="saveBindDistrict"><i class="el-icon-eleg-icon_drive el-icon--left"></i>保存</el-button>
					</div>
				</div>
				<div style="width: 100%">
					<el-tree :show-overflow-tooltip="true" ref="tree" :data="tableDatas" show-checkbox :props="defaultProps" node-key="DistrictKeyId" :default-expanded-keys="defaultKey" :default-checked-keys="defaultCheckedKeys">
					</el-tree>
				</div>
			</el-tab-pane>
		</el-tabs>

		<!-- 所有未绑定图书列表 -->
		<el-dialog title="馆藏地未绑定图书列表" v-model="bindBookDialog" :class="[dialog-large-width]">
			<div style="padding: 3px 0;">
				<span style="font-size: 13px">查询</span>
				<el-select size="small" v-model="searchBookType" placeholder="请选择查询方式" style="width: 100px;margin-right: 2px">
					<el-option label="书名" :value="1"></el-option>
					<el-option label="条码" :value="2"></el-option>
					<el-option label="标签" :value="3"></el-option>
				</el-select>
				<el-input v-model="searchBookModel" size="small" style="width: 200px;">
					<el-button slot="append" icon="search" @click="searchUnBindBook"></el-button>
				</el-input>
			</div>
			<el-table border style="width: 100%;" @selection-change="handleUnBindBookSelectionChange" :data="unBindBookData">
				<el-table-column type="selection" align="center" width="50px"></el-table-column>
				<el-table-column label="书名" align="center" prop="Name"></el-table-column>
				<el-table-column label="作者" align="center" prop="Author"></el-table-column>
				<el-table-column label="条码" align="center" prop="BookBarcode"></el-table-column>
				<el-table-column label="标签" align="center" prop="RFIDTag"></el-table-column>
				<el-table-column label="绑定时间" align="center" prop="BindTime"></el-table-column>
				<el-table-column label="状态" align="center">
					<template slot-scope="scope">
						<el-tag type="danger" v-if="scope.row.Status === 1">丢失</el-tag>
						<el-tag type="warning" v-if="scope.row.Status === 2">外借</el-tag>
						<el-tag type="success" v-if="scope.row.Status === 3">在库</el-tag>
					</template>
				</el-table-column>
			</el-table>
			<div slot="footer" class="dialog-footer">
				<div class="block" style="text-align:right;padding-top: 3px;padding-bottom: 10px;">
					<el-pagination @size-change="handleUnBindBookSizeChange" @current-change="handleUnBindBookCurrentChange" :current-page="unBindBookCurrentPage" :page-size="unBindBookPageSize" :page-sizes="[10,20,30,40,50,70,100]" layout="total,sizes, prev, pager, next, jumper" :total="unBindBookTotalSize">
					</el-pagination>
				</div>
				<el-button @click="bindBookDialog = false">取 消</el-button>
				<el-button type="primary" @click="confirmBindBook">确 定</el-button>
			</div>
		</el-dialog>

		<!-- 所有未绑定读者列表 -->
		<el-dialog title="馆藏地未绑定读者列表" v-model="bindReaderDialog" :class="[dialog-large-width]">
			<div style="padding: 3px 0;">
				<span style="font-size: 13px">查询</span>
				<el-select size="small" v-model="searchReaderType" placeholder="请选择查询方式" style="width: 100px;margin-right: 2px">
					<el-option label="姓名" :value="1"></el-option>
					<el-option label="读者证号" :value="2"></el-option>
					<el-option label="标签" :value="3"></el-option>
					<el-option label="角色" :value="4"></el-option>
				</el-select>
				<el-input v-model="searchReaderModel" size="small" style="width: 200px;">
					<el-button id="searchData" slot="append" icon="search" @click="searchUnBindReader"></el-button>
				</el-input>
			</div>
			<el-table border style="width: 100%;" @selection-change="handleUnBindReaderSelectionChange" :data="unBindReaderData">
				<el-table-column type="selection" align="center" width="50px"></el-table-column>
				<el-table-column label="读者姓名" align="center" prop="Name"></el-table-column>
				<el-table-column label="读者证号" align="center" prop="ReaderCode"></el-table-column>
				<el-table-column label="读者标签" align="center" prop="ReaderTagCode"></el-table-column>
				<el-table-column label="角色" align="center" prop="Type"></el-table-column>
				<el-table-column label="创建时间" align="center" prop="CreateTime"></el-table-column>
			</el-table>
			<div slot="footer" class="dialog-footer">
				<div class="block" style="text-align:right;padding-top: 3px;padding-bottom: 10px;">
					<el-pagination @current-change="handleUnBindReaderCurrentChange" :current-page="unBindReaderCurrentPage" @size-change="handleUnBindReaderSizeChange" :page-size="unBindReaderPageSize" :page-sizes="[10,20,30,40,50,70,100]" layout="total,sizes, prev, pager, next, jumper" :total="unBindReaderTotalSize">
					</el-pagination>
				</div>
				<el-button @click="bindReaderDialog = false">取 消</el-button>
				<el-button type="primary" @click="confirmBindReader">确 定</el-button>
			</div>
		</el-dialog>

		<!-- 修改图书馆藏地 -->
		<el-dialog title="馆藏地列表" v-model="setBookLocationDialog" :class="[dialog-large-width]">
			<el-table border style="width: 100%;" :data="locationDatas">
				<el-table-column label="" width="50">
					<template slot-scope="scope">
						<el-radio :label="scope.row.Name" v-model="locationName" @change.native="getCurrentRow(scope.row)"></el-radio>
					</template>
				</el-table-column>
				<el-table-column label="名称" align="center" prop="Name"></el-table-column>
				<el-table-column label="创建时间" align="center" prop="CreateTime"></el-table-column>
				<el-table-column label="创建人" align="center" prop="CreateUid"></el-table-column>
			</el-table>
			<div class="block" style="text-align: right;padding-top: 2px">
				<el-pagination @current-change="handleLocationCurrentChange" @size-change="handleLocationSizeChange" :current-page="locationCurrentPage" :page-size="locationPageSize" :page-sizes="[10,20,30,40,50]" layout="total,sizes, prev, pager, next, jumper" :total="locationTotalSize">
				</el-pagination>
			</div>
			<span slot="footer" class="dialog-footer">
				<el-button @click="setBookLocationDialog = false">取 消</el-button>
				<el-button type="primary" @click="confirmBindLocation">确 定</el-button>
			</span>
		</el-dialog>
	</div>
</template>
<script>
	import $ from 'jquery'
	import { GloabFun, HttpServer } from '../../../../assets/js/common.js'
	import headerOpreate from '../../../../components/headerOpreate'
	let id = 1000;
	export default {
		components: {
			headerOpreate
		},
		data() {
			return {
				loading: false,
				activeName: "first",

				//用户绑定的馆藏地
				locationDatas: [],
				locationObj: {},
				locationName: '',
				locationCurrentPage: 1,
				locationPageSize: 10,
				locationTotalSize: 0,
				setBookLocationDialog: false,

				isReloadBookInfo: false,
				bookValue: 0,
				bookSearchModel: '',
				bindBookData: [], //绑定的图书信息
				bindBookCurrentPage: 1, //已绑定图书当前页面
				bindBookPageSize: 10, //已绑定图书页面大小
				bindBookTotalSize: 0,
				multipleBindBookSelection: [], //批量选择已绑定图书信息，用户删除

				bindBookDialog: false, //绑定图书弹窗
				searchBookType: 1,
				searchBookModel: '',
				unBindBookData: [], //未绑定图书信息
				unBindBookCurrentPage: 1, //未绑定图书当前页码
				unBindBookPageSize: 10, //未绑定图书页面大小
				unBindBookTotalSize: 0,
				multipleUnBindBookSelection: [], //选择的未绑定图书，用于添加


				readerValue: 0,
				readerSearchModel: '',
				bindReaderData: [], //绑定的读者信息
				bindReaderCurrentPage: 1, //已绑定读者当前页面
				bindReaderPageSize: 10, //已绑定读者页面大小
				bindReaderTotalSize: 0,
				multipleBindReaderSelection: [], //批量选择已绑定读者信息，用户删除

				isReloadReaderInfo: false,
				bindReaderDialog: false, //绑定读者弹窗
				searchReaderType: 1,
				searchReaderModel: '',
				unBindReaderData: [], //未绑定读者信息
				unBindReaderTotalSize: 0,
				unBindReaderCurrentPage: 1, //未绑定读者当前页码
				unBindReaderPageSize: 10, //未绑定读者页面大小
				multipleUnBindReaderSelection: [], //选择的未绑定读者，用于添加

				//绑定区域相关
				isReloadDistrictInfo: false,
				tableDatas: [],
				defaultProps: {
					children: 'Children',
					label: 'Name',
					id: 'DistrictKeyId'
				},
				defaultKey: [],
				defaultCheckedKeys: []
			}
		},
		props: {
			templateObj: {
				type: Object,
				default: function() {
					return {};
				}
			}
		},
		methods: {
			executeAction(action) {
				if(action) {
					this[action]();
				}
			},
			//tab切换
			handleClick(tab, event) {
				if(tab.name === 'first') {
					this.getBindBookInfo();
				} else if(tab.name === 'second') {
					this.getBindReaderInfo();
				}else {
					this.getBindDistrictInfo();
				}
			},
			/**
			 *   读者管理
			 **/
			//查看馆藏地已绑定读者
			getBindReaderInfo() {
				if(this.readerValue === 0){
					var searchStr = '&name=' +this.readerSearchModel+ '&code=&tag=&type=';
				}else if(this.readerValue === 1){
					var searchStr = '&name=&code=' +this.readerSearchModel+ '&tag=&type=';
				}else if(this.readerValue === 2){
					var searchStr = '&name=&code={code}&tag=' +this.readerSearchModel+ '&type=';
				}else{
					var searchStr = '&name=&code=&tag=&type=' +this.readerSearchModel;
				}
				
				HttpServer.httpGet.call(this, '/Api/LibraryLocation/ReaderInfo?locationKeyId=' + this.templateObj.LocationKeyId + '&index=' + this.bindReaderCurrentPage + '&pagesize=' + this.bindReaderPageSize + searchStr, function(response) {}, function(response) {
					if(response.data.code === 0) {
						this.bindReaderData = response.data.readerinfo;
						this.bindReaderTotalSize = response.data.total;
						if(this.isReloadReaderInfo){
							this.$notify({
								title: '成功',
								message: this.$t('message.refreshSuccess'),
								type: 'success',
								duration: 2500
							});
						}
					} else {
						this.$notify.error({
							title: '错误',
							message: response.data.msg,
							duration: 2500
						});
					}
					this.isReloadReaderInfo = false;
				})
			},
			//刷新绑定读者信息
			reloadReaderInfo() {
				this.isReloadReaderInfo = true;
				this.getBindReaderInfo();
			},
			//切换已绑定读者当前页面
			handleBindReaderCurrentChange(val) {
				this.bindReaderCurrentPage = val;
				this.getBindReaderInfo();
			},
			//切换已绑定读者数据大小
			handleBindReaderSizeChange(val) {
				this.bindReaderPageSize = val;
				this.getBindReaderInfo();
			},
			//批量选择已绑定读者
			handleBindReaderSelectionChange(val) {
				this.multipleBindReaderSelection = val;
			},
			//组装所选已绑定读者数据格式
			formateBindReaderSelection() {
				var result = [],
				array = this.multipleBindReaderSelection;
				for(var i = 0; i < array.length; i++) {
					result.push(array[i].ReaderInfoKeyId)
					result[i] = {};
					result[i].locationKeyId = this.templateObj.LocationKeyId;
					result[i].readerkeyid = array[i].ReaderInfoKeyId;
				}
				return result;
			},
			//删除已绑定读者
			deleteReader() {
				if(this.formateBindReaderSelection().length > 0) {
					this.$confirm('此操作将解除所选读者与馆藏地的绑定关系，是否继续？', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						HttpServer.httpPost.call(this, '/Api/LibraryLocation/UnBindReader', {
							info: this.formateBindReaderSelection()
						}, function(response) {}, function(response) {
							if(response.data.code === 0) {
								this.getBindReaderInfo();
								this.$notify({
									title: '成功',
									message: this.$t('message.deleteSuccess'),
									type: 'success',
									duration: 2500
								});
							} else {
								this.$notify.error({
									title: '错误',
									message: response.data.msg,
									duration: 2500
								});
							}
						})
					}).catch(() => {
						this.$notify.info({
							title: '消息',
							message: this.$t('message.operationCanceled'),
							duration: 2500
						})
					});
				} else {
					this.$notify({
						title: this.$t('message.warning'),
						message: this.$t('message.selectBulkDeleteWarning'),
						type: 'warning',
						duration: 2500
					});
				}
			},

			/**
			 *   未绑定读者管理
			 **/
			//打开绑定读者弹窗
			bindReader() {
				this.unBindReaderCurrentPage = 1;
				this.unBindReaderPageSize = 10;
				this.bindReaderDialog = true;
				this.getUnBindReaderInfo();
			},
			//切换未绑定读者页码
			handleUnBindReaderCurrentChange(val) {
				this.unBindReaderCurrentPage = val;
				this.getUnBindReaderInfo();
			},
			handleUnBindReaderSizeChange(val) {
				this.unBindReaderPageSize = val;
				this.getUnBindReaderInfo();
			},
			//搜索
			searchUnBindReader() {
				this.getUnBindReaderInfo();
			},
			//去掉时间中的T
			formateUnBindReaderData(array) {
				for(var i = 0; i < array.length; i++) {
					var index1 = array[i].CreateTime.indexOf("T");
					array[i].CreateTime = array[i].CreateTime.substring(0, index1) + " " + array[i].CreateTime.substring(index1 + 1, index1 + 9);
				}
			},
			//查看所有未绑定到该区域的读者
			getUnBindReaderInfo() {
				HttpServer.httpGet.call(this, '/Api/LibraryLocation/UnbindReader?locationKeyId=' + this.templateObj.LocationKeyId + '&index=' + this.unBindReaderCurrentPage + '&pagesize=' +
					this.unBindReaderPageSize + '&type=' + this.searchReaderType + '&filter=' + this.searchReaderModel,
					function(response) {},
					function(response) {
						// console.log(response)
						if(response.data.code === 0) {
							this.formateUnBindReaderData(response.data.readerinfo);
							this.unBindReaderTotalSize = response.data.total;
							this.unBindReaderData = response.data.readerinfo;
						} else {
							this.$notify.error({
								title: '错误',
								message: response.data.msg,
								duration: 2500
							});
						}
					})
			},
			//批量选择未绑定读者
			handleUnBindReaderSelectionChange(val) {
				this.multipleUnBindReaderSelection = val;
			},
			//组装所选未绑定读者数据格式
			formateUnBindReaderSelection() {
				var result = [],
				array = this.multipleUnBindReaderSelection;
				for(var i = 0; i < array.length; i++) {
					result[i] = {};
					result[i].locationkeyId = this.templateObj.LocationKeyId;
					result[i].readerkeyid = array[i].ReaderInfoKeyId;
				}
				return result;
			},
			//添加绑定
			confirmBindReader() {
				if(this.formateUnBindReaderSelection().length > 0) {
					this.$confirm('此操作将所选读者与该馆藏地进行绑定，是否继续？', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						HttpServer.httpPost.call(this, '/Api/LibraryLocation/BindReader', {
							info: this.formateUnBindReaderSelection(),
							UserId: parseInt(GloabFun.getKeyId())
						}, function(response) {}, function(response) {
							if(response.data.code === 0) {
								this.getUnBindReaderInfo();
								this.getBindReaderInfo();
								this.$notify({
									title: '成功',
									message: this.$t('message.saveSuccess'),
									type: 'success',
									duration: 2500
								});
							} else {
								this.getUnBindReaderInfo();
								this.getBindReaderInfo();
								this.$notify.error({
									title: '错误',
									message: response.data.msg,
									duration: 2500
								});
							}
						})
					}).catch(() => {
						this.$notify.info({
							title: '消息',
							message: this.$t('message.operationCanceled'),
							duration: 2500
						})
					});
				} else {
					this.$notify({
						title: this.$t('message.warning'),
						message: this.$t('message.selectBulkAddWarning'),
						type: 'warning',
						duration: 2500
					});
				}
			},

			/**
			 *   已绑定图书管理
			 **/
			//查询馆藏地绑定图书信息
			getBindBookInfo() {
				if(this.bookValue === 0){
					var searchStr = '&keyword=' +this.bookSearchModel+ '&barcode=&tag=';
				}else if(this.bookValue === 1){
					var searchStr = '&keyword=&barcode=' +this.bookSearchModel+ '&tag=';
				}else{
					var searchStr = '&keyword=&barcode=&tag=' +this.bookSearchModel;
				}
				
				this.loading = true;
				HttpServer.httpGet.call(this, '/Api/LibraryLocation/Bookinfo?locationKeyId=' +this.templateObj.LocationKeyId+ '&index=' +this.bindBookCurrentPage+ '&pagesize=' +this.bindBookPageSize+ searchStr, function(response) {}, function(response) {
					this.loading = false;
					if(response.data.code === 0) {
						this.bindBookData = response.data.bookinfo;
						this.bindBookTotalSize = response.data.total;
						if(this.isReloadBookInfo){
							this.$notify({
								title: '成功',
								message: this.$t('message.refreshSuccess'),
								type: 'success',
								duration: 2500
							});
						}
					} else {
						this.$notify.error({
							title: '错误',
							message: response.data.msg,
							duration: 2500
						});
					}
					this.isReloadBookInfo = false;
				})
			},
			//刷新绑定图书信息
			reloadBookInfo() {
				this.isReloadBookInfo = true;
				this.getBindBookInfo();
			},
			//修改查询馆藏地绑定图书信息页码
			handleBindBookCurrentChange(val) {
				this.bindBookCurrentPage = val;
				this.getBindBookInfo();
			},
			//修改查询馆藏地绑定图书信息大小
			handleBindBookSizeChange(val) {
				this.bindBookPageSize = val;
				this.getBindBookInfo();
			},
			//批量选择已绑定图书
			handleBindBookSelectionChange(val) {
				this.multipleBindBookSelection = val;
			},
			//组装选择已绑定图书数据
			formateBindBookSelection() {
				var result = [],
				array = this.multipleBindBookSelection;
				for(var i = 0; i < array.length; i++) {
					result[i] = {};
					result[i].locationkeyId = this.templateObj.LocationKeyId;
					result[i].bookKeyId = array[i].BookId;
				}
				return result;
			},
			//批量修改图书馆藏地
			changeBookLocation() {
				if(this.formateBindBookSelection.length > 0){
					this.setBookLocationDialog = true;
					this.getAllLocationDatas();
				}else {
					this.$notify({
						title: this.$t('message.warning'),
						message: '请选择批量修改的图书',
						type: 'warning',
						duration: 2500
					});
				}
			},
			confirmBindLocation() {
				this.$confirm('此操作将所选图书与' +this.locationObj.Name+ '进行绑定，是否继续？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					var result = [];
					for(var i = 0 ; i < this.multipleBindBookSelection.length ; i ++){
						result[i] = {};
						result[i].locationkeyId = this.locationObj.LocationKeyId;
						result[i].bookkeyId = this.multipleBindBookSelection[i].BookId;
					}
					HttpServer.httpPost.call(this, '/Api/LibraryLocation/BindBook', {
						info: result
					}, function(response) {}, function(response) {
						if(response.data.code === 0) {
							this.getBindBookInfo();
							this.setBookLocationDialog = false;
							this.$notify({
								title: '成功',
								message: this.$t('message.saveSuccess'),
								type: 'success',
								duration: 2500
							});
						} else {
							this.$notify.error({
								title: '错误',
								message: response.data.msg,
								duration: 2500
							});
						}
					});
				}).catch(() => {
					this.$notify.info({
						title: '消息',
						message: this.$t('message.operationCanceled'),
						duration: 2500
					})
				});			
			},
			//单选
			getCurrentRow(row) {
				this.locationObj = row;
				this.locationName = row.Name;
			},
			//去掉时间中的T
			formateLocationData(array) {
				for(var i = 0; i < array.length; i++) {
					var index1 = array[i].CreateTime.indexOf("T");
					var index2 = array[i].UpdateTime.indexOf("T");
					array[i].CreateTime = array[i].CreateTime.substring(0, index1) + " " + array[i].CreateTime.substring(index1 + 1, index1 + 9);
					array[i].UpdateTime = array[i].UpdateTime.substring(0, index2) + " " + array[i].UpdateTime.substring(index2 + 1, index2 + 9);
				}
			},
			handleLocationCurrentChange(val) {
				this.locationCurrentPage = val;
				this.getAllLocationDatas();
			},
			handleLocationSizeChange(val) {
				this.locationPageSize = val;
				this.getAllLocationDatas();
			},
			//获取当前用户绑定的馆藏地
			getAllLocationDatas() {
				HttpServer.httpPost.call(this, '/Api/LibraryLocation/List', {
					UserId: parseInt(GloabFun.getKeyId()),
					index: this.locationCurrentPage,
					pagesize: this.locationPageSize,
					districtid: 0,
					keyword: ''
				}, function(response) {}, function(response) {
					this.loading = false;
					if(response.data.code === 0) {
						this.formateLocationData(response.data.info);
						this.locationDatas = response.data.info;
						this.locationObj = response.data.info[0];
						this.locationName = response.data.info[0].Name;
						this.locationTotalSize = response.data.total;
					} else {
						this.$notify.error({
							title: '错误',
							message: response.data.msg,
							duration: 2500
						});
					}
				});
			},
			//删除已绑定图书
			deleteBook() {
				if(this.formateBindBookSelection().length > 0) {
					this.$confirm('此操作将解除所选书籍与馆藏地的绑定关系，是否继续？', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						HttpServer.httpPost.call(this, '/Api/LibraryLocation/UnBindBook', {
							_info: this.formateBindBookSelection()
						}, function(response) {}, function(response) {
							if(response.data.code === 0) {
								this.getBindBookInfo();
								this.$notify({
									title: '成功',
									message: this.$t('message.deleteSuccess'),
									type: 'success',
									duration: 2500
								});
							} else {
								this.$notify.error({
									title: '错误',
									message: response.data.msg,
									duration: 2500
								});
							}
						})
					}).catch(() => {
						this.$notify.info({
							title: '消息',
							message: this.$t('message.operationCanceled'),
							duration: 2500
						})
					});
				} else {
					this.$notify({
						title: this.$t('message.warning'),
						message: this.$t('message.selectBulkDeleteWarning'),
						type: 'warning',
						duration: 2500
					});
				}
			},
			/**
			 *   未绑定图书管理
			 **/
			//绑定图书弹窗
			bindBook() {
				this.unBindBookCurrentPage = 1;
				this.unBindBookPageSize = 10;
				this.bindBookDialog = true;
				this.getUnBindBookInfo();
			},
			//修改未绑定图书当前页码
			handleUnBindBookCurrentChange(val) {
				this.unBindBookCurrentPage = val;
				this.getUnBindBookInfo();
			},
			handleUnBindBookSizeChange(val) {
				this.unBindBookPageSize = val;
				this.getUnBindBookInfo();
			},
			searchUnBindBook() {
				this.getUnBindBookInfo();
			},
			//去掉时间中的T
			formateUnBindBookData(array) {
				for(var i = 0; i < array.length; i++) {
					var index1 = array[i].BindTime.indexOf("T");
					array[i].BindTime = array[i].BindTime.substring(0, index1) + " " + array[i].BindTime.substring(index1 + 1, index1 + 9);
				}
			},
			//查询未绑定图书
			getUnBindBookInfo() {
				HttpServer.httpGet.call(this, '/Api/LibraryLocation/UnbindBookinfo?index=' + this.unBindBookCurrentPage + '&pagesize=' + this.unBindBookPageSize + '&type=' + this.searchBookType + '&filter=' + this.searchBookModel, function(response) {}, function(response) {
					// console.log('未绑定图书信息',response)
					if(response.data.code === 0) {
						this.formateUnBindBookData(response.data.bookinfo);
						this.unBindBookData = response.data.bookinfo;
						this.unBindBookTotalSize = response.data.total;
					} else {
						this.$notify.error({
							title: '错误',
							message: response.data.msg,
							duration: 2500
						});
					}
				})
			},
			//批量选择未绑定图书
			handleUnBindBookSelectionChange(val) {
				this.multipleUnBindBookSelection = val;
			},
			//组装未绑定图书数据格式
			formateUnBindBookSelection() {
				var result = [],
				array = this.multipleUnBindBookSelection;
				for(var i = 0; i < array.length; i++) {
					result[i] = {};
					result[i].locationkeyId = this.templateObj.LocationKeyId;
					result[i].bookkeyId = array[i].BookId;
				}
				return result;
			},
			//确定绑定图书
			confirmBindBook() {
				if(this.formateUnBindBookSelection().length > 0) {
					this.$confirm('此操作将所选图书与该馆藏地进行绑定，是否继续？', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						HttpServer.httpPost.call(this, '/Api/LibraryLocation/BindBook', {
							info: this.formateUnBindBookSelection()
						}, function(response) {}, function(response) {
							if(response.data.code === 0) {
								this.getUnBindBookInfo();
								this.getBindBookInfo();
								this.$notify({
									title: '成功',
									message: this.$t('message.saveSuccess'),
									type: 'success',
									duration: 2500
								});
							} else {
								this.getUnBindBookInfo();
								this.getBindBookInfo();
								this.$notify.error({
									title: '错误',
									message: response.data.msg,
									duration: 2500
								});
							}
						})
					}).catch(() => {
						this.$notify.info({
							title: '消息',
							message: this.$t('message.operationCanceled'),
							duration: 2500
						})
					});
				} else {
					this.$notify({
						title: this.$t('message.warning'),
						message: this.$t('message.selectBulkAddWarning'),
						type: 'warning',
						duration: 2500
					});
				}
			},

			/**
			 *   已绑定区域管理
			 **/
			//由于树控件在父级选中时，所有子项会默认选中，不符合需求，所以需要从数组中删除所有含有子项的父级
			traverseTree(arr1,arr2){
				//arr1为默认选中的数,arr2为区域信息数组
				for (var m = 0 ; m < arr1.length; m ++){
					for(var n = 0 ; n < arr2.length; n ++){
						if(arr1[m] == arr2[n].DistrictKeyId && arr2[n].Children.length > 0){
							arr1.splice(m,1);
							this.traverseTree(arr1,arr2[n].Children);
						}
					}
				}	
			},
			//查询所有区域信息
			getBindDistrictInfo() {
				HttpServer.httpPost.call(this, '/Api/District/info', {
					keyids: []
				}, function(response) {}, function(response) {
					if(response.data.code === 0) {
						if(response.data.info) {
							HttpServer.httpPost.call(this,'/Api/LibraryLocation/Binded/District',{locationid: this.templateObj.LocationKeyId,UserId: parseInt(GloabFun.getKeyId())}, function(res){},function(res){
								if(response.data.code === 0){
									this.traverseTree(res.data.info, response.data.info);
									this.tableDatas = response.data.info;
									this.defaultKey = [this.tableDatas[0].DistrictKeyId];
									this.defaultCheckedKeys = res.data.info;
									if(this.isReloadDistrictInfo){
										this.$notify({
											title: '成功',
											message: this.$t('message.refreshSuccess'),
											type: 'success',
											duration: 2500
										});
									}
								}else {
									this.$notify.error({
										title: '错误',
										message: response.data.msg,
										duration: 2500
									});
								}
								this.isReloadDistrictInfo = false;
							})
						}
					} else {
						this.$notify.error({
							title: '错误',
							message: response.data.msg,
							duration: 2500
						});
					}
				});
			},
			//保存绑定
			saveBindDistrict() {
				this.$confirm('此操作将保存馆藏地绑定的区域信息，是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					HttpServer.httpPost.call(this, '/Api/LibraryLocation/Bind/District', {
						locationid: this.templateObj.LocationKeyId,
						districtids: this.$refs.tree.getCheckedKeys(),
						UserId: parseInt(GloabFun.getKeyId())
					}, function(response) {}, function(response) {
						if(response.data.code === 0) {
							this.$notify({
								title: '成功',
								message: this.$t('message.saveSuccess'),
								type: 'success',
								duration: 2500
							});
							this.getBindDistrictInfo();
						} else {
							this.$notify.error({
								title: '错误',
								message: response.data.msg,
								duration: 2500
							});
						}
					});
				}).catch(() => {
					this.$notify.info({
						title: '消息',
						message: this.$t('message.cancleOperation'),
						duration: 2500
					})
				});
			},
			//刷新绑定区域信息
			reloadDistrictInfo() {
				this.isReloadDistrictInfo = true;
				this.getBindDistrictInfo();
			},

		},
		created() {
			this.getBindBookInfo();
		}
	}
</script>
<style scoped lang="less">
	.flex-inline {
		display: flex;
		justify-content: space-between;
		padding-bottom: 2px;
		.query {
			display: flex;
		}
	}

	.name {
		color: #20a0ff;
	}

	.edit-mission {
		padding-top: 50px;
		.el-dialog__header {
			background-color: #000;
		}
		.el-dialog__body {
			position: relative;
		}
		.clearfloat {
			height: 100px;
			max-height: 150px;
		}
	}
</style>